package com.flink.flinkdemo.demo;

import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author zhangy on 2021/2/3
 * @description: add you description
 */
public class ImgDealDemo {

    public static void main(String[] args) throws Exception {
        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<String> dataSource = env.readTextFile("/Users/zhangyong/Idea-git/flink-d/flink-demo/src/doc/imgt1");
        dataSource.print();
        dataSource.map(new MapFunction<String, String>() {
            @Override
            public String map(String value) throws Exception {
                String[] split = value.split(",");
                for (String s : split) {
                    if(s.contains("img_url")){
                        String[] words = s.split("url");
                        if(words.length>1){
                            return words[1].replace("\\\":\\\"","").replace("\\\"","");
//                                    .replace(":","%3A%2F%2F").replace("/","%2F");
                        }
                    }
                }
                return value;
            }
        }).print();
        env.execute();
    }


//    public static void main(String[] args) throws IOException {
//        String urlStr = "http://imgt.wpt.la/qrcode/multi-scan?pic=http://bj-video-1251671073.cos.ap-beijing.myqcloud.com/image/20210130/a2f702ce764ae30dea02c64c53f57a89_v0.jpg";
//        URL url = new URL(urlStr);
//        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
//        httpCon.setConnectTimeout(3000);
//        httpCon.setDoInput(true);
//        httpCon.setRequestProperty("appid","123");
////        httpCon.setRequestProperty("pic","http://bj-video-1251671073.cos.ap-beijing.myqcloud.com/image/20210130/a2f702ce764ae30dea02c64c53f57a89_v0.jpg");
//        httpCon.setRequestMethod("GET");
//
//        System.out.println(JSONObject.toJSONString(httpCon.getInputStream()));
//    }
}